﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DotSpider.Controls;
using System.Collections.Generic;

namespace dot_searcher
{
    public partial class _Default : System.Web.UI.Page
    {
        private string LabelContent;
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<center><h2>dotSearhcer (r)</h2></center>");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Settings.Database.DbConfigFile = Server.MapPath("DbSettings.xml");
            MySQL db = new MySQL();
            db.Connect();
            Dictionary<string, int> queryWords = 
                Indexer.ExtractWords(string.Format(" {0} ", TextBox1.Text));
            Dictionary<KeyValuePair<string, string>, Dictionary<string, int>> table = 
                db.SearchWords(queryWords);
            LabelContent = "";
            int pos = 1;
            
            foreach (KeyValuePair<KeyValuePair<string, string>, Dictionary<string, int>> entry in table)
            {
                KeyValuePair<string, string> addKey = (KeyValuePair<string, string>)entry.Key;
                Dictionary<string, int> words = (Dictionary<string, int>) entry.Value;
                /// Hashtable <KeyValuePair address caption, Dictionary<word, quantity>>
                /// Output searched record.
                string add = string.Format(
                    "<b>{0}. {1}</b><br/><a href=\"{2}\">{2}</a><br/>",
                    pos++, 
                    addKey.Value,
                    addKey.Key);
                /// Output words.
                foreach (KeyValuePair<string, int> word in words)
                {
                    add += string.Format("<i>{0} ({1})</i>&nbsp;", word.Key, word.Value);
                }
                LabelContent += add + "<br/>";

                /// DEMO
                if (pos > 20)
                {
                    LabelContent += "<br/><b> End of first 20 records.  Demo version.</b>";
                    break;
                }
            }
            if (LabelContent.Length < 5) LabelContent = "<b>Not found</b>";
            Response.Write(LabelContent);
        }
    }
}
